'''
Author: 风云 mayude@live.cn
Date: 2021-12-08 17:08:02
LastEditors: 风云 mayude@live.cn
LastEditTime: 2022-05-22 16:16:17
FilePath: /tinyms/tinyms-api/apps/cms/articlechannel/service.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
from app import db
from .model import ChannelArticle

class ChannelArticleService:
    """
    保存数据
    """
    def save(channelarticle):
        db.session.add(channelarticle)
        db.session.commit()
        
    '''
    更新数据
    '''
    def update(channelarticle):
        tmp_channelarticle = ChannelArticle.query.get(channelarticle.channel_pk)
        tmp_channelarticle.fk_channel_pk = channelarticle.fk_channel_pk
        tmp_channelarticle.fk_article_pk = channelarticle.fk_article_pk
        db.session.commit()
    
    '''
    删除数据
    '''
    def delete(fk_article_pk):
        filterList = []
        if fk_article_pk is not None:
            filterList.append(ChannelArticle.fk_article_pk==fk_article_pk)
        channelArticles = ChannelArticle.query.filter(*filterList).all()
        if channelArticles:
            for channelarticle in channelArticles:
                db.session.delete(channelarticle)
        db.session.commit()

    '''
    查询
    '''
    def list(channelarticle):
        filterList = []
        if channelarticle.fk_article_pk is not None:
            filterList.append(ChannelArticle.fk_article_pk==channelarticle.fk_article_pk)

        channelArticle = ChannelArticle.query.filter(*filterList).all()
        return channelArticle